﻿<%@ WebHandler Language="C#" Class="UpdateState" %>

using System;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.SessionState;
//using JCKJ.DBUtility;
using BLL;
using jmail;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SqlHelperLjr;

public class UpdateState : IHttpHandler, IRequiresSessionState
{
    //public static HttpContext context;

    //public void sqlEmail()
    //{
    //    string sqlEmail = "select distinct(b.Email) from Tbl_WayBill a "
    //                    + "join TblUserInfo b on a.Sender = b.TrueName "
    //                    + "where a.Sender = '"+ context.Request.Params["jjr"] +"'";

    //    DataTable dtEmail = new SqlHelper().Query(sqlEmail).Tables[0];
    //    Dictionary<string, string> dicEmail = new Dictionary<string, string>();
    //    for (int i = 0; i < dtEmail.Rows.Count; i++)
    //    {
    //        dicEmail.Clear();
    //        dicEmail.Add("Email", dtEmail.Rows[i]["Email"].ToString());
    //    }
    //}

    
    public void ProcessRequest(HttpContext context)
    {
        //MessageBox.Show("11111");


        context.Response.ContentType = "text/plain";
        Hashtable hs = new Hashtable();
        Tools tool = new Tools();
        hs.Clear();
        hs.Add("SepNo", context.Request.Params["Tno"]);
        hs.Add("Date", tool.StringToTime(context.Request.Params["ndate"].ToString() + " 00:00:01 "));
        hs.Add("StateID", context.Request.Params["state"]);
        hs.Add("RecordDate", DateTime.Now);
        hs.Add("RecorderID", context.Session["uid"].ToString());
        hs.Add("CompanyID", tool.StringToIntDif(context.Request.Params["gs"]));
        hs.Add("CompanyNo", context.Request.Params["zy"]);
        hs.Add("Remark", context.Request.Params["bz"]);
        
        //===============================================================================
        //try
        //{
            


        //}
        //catch (Exception ex)
        //{

        //    MessageBox.Show(ex.Message);
        //}
        //===============================================================================
        
        if (new PublicSqlService().Insert(hs, "Tbl_BillStateRec"))
        {
            if (context.Request.Params["sep"] == null)
            {
                hs.Clear();

                hs.Add("StateId", context.Request.Params["state"]);
                hs.Add("UPEXState", context.Request.Params["Sname"]);
                new PublicSqlService().Update(hs, "Tbl_WayBill", "TrackingNo='" + context.Request.Params["Tno"].Trim() + "'");

            }
            else
            {
                hs.Clear();

                hs.Add("State", context.Request.Params["state"]);
                hs.Add("SBState", context.Request.Params["Sname"]);
                new PublicSqlService().Update(hs, "Tbl_SepBill", "SepNo='" + context.Request.Params["Tno"].Trim() + "'");
                if (context.Request.Params["state"].Trim() != "1")
                {
                    //更新主单状态
                    new WayBillService().UpSatate(context.Request.Params["Tno"].Trim());
                }
            }
            context.Response.Write("ok");
            //sendEmail("wyj361978548@126.com", "wyj361978548@126.com", "", "", dicEmail.ToString(), "运单状态更改通知（" + DateTime.Now.ToLongTimeString().ToCharArray() + ")", "尊敬的客户：您的运单状态以更改！", "");

                sendEmail("lam@upexusa.net", "lam@upexusa.net", "", "", "21980338@qq.com", "运单状态更改通知(" + context.Request.Params["Tno"] + ")", "尊敬的客户" + context.Request.Params["Tno"] + "您好：您的运单号：" + context.Request.Params["Tno"] + "状态已更改，现在的状态为：" + context.Request.Params["Sname"] + ".", "");
        }
        else
        {
            context.Response.Write("失败");
        }

    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
    
    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender">发件人邮箱</param>
    /// <param name="senderuser">发件人姓名</param>
    /// <param name="euser">发件用户组</param>
    /// <param name="epwd">发件用户组密码</param>
    /// <param name="receiver">收件人</param>
    /// <param name="subject">主题</param>
    /// <param name="body">正文</param>
    /// <param name="eserver">发送邮件服务器</param>
    public void sendEmail(string sender, string senderuser, string euser, string epwd, string receiver, string subject, string body, string eserver)
    {
        try
        {
            jmail.MessageClass jmessage = new jmail.MessageClass();
            jmessage.Charset = "GB2312";
            jmessage.From = sender;   //发信地址
            jmessage.FromName = senderuser;   //发信人
            //jmessage.ReplyTo = "wyj361978548@126.com";   //回复地址
            jmessage.Subject = subject;  //邮件标题
            jmessage.AddRecipient(receiver, "", "");  //为收信人地址，后面两个参数可为空！
            //AddRecipient(emailAddress,recipientName,PGPKey) 
            //给邮件添加一个收件人。RecipientName和PGPKey是可选项，RecipientName为收件人姓名，PGPKey给邮件加密。
            jmessage.Body = body;  //信件内容
            jmessage.MailServerUserName = euser;   //为发信邮箱登陆的用户名
            jmessage.MailServerPassWord = epwd;   //为发信邮箱登陆的密码
            jmessage.Send(eserver, false);  //IP为邮件服务器
            //Response.Write("email sent successfully!");
            jmessage.Close();
        }
        catch (Exception ex)
        {

            MessageBox.Show(ex.Message);
        }
    }
}